package com.xuebusi.toutiao.admin.api.user.controller;

import com.xuebusi.toutiao.admin.api.common.annotation.LoginUser;
import com.xuebusi.toutiao.admin.api.common.result.ApiResult;
import com.xuebusi.toutiao.admin.api.common.result.PageInfo;
import com.xuebusi.toutiao.admin.api.user.entity.SourceEntity;
import com.xuebusi.toutiao.admin.api.user.entity.UserEntity;
import com.xuebusi.toutiao.admin.api.user.form.LoginForm;
import com.xuebusi.toutiao.admin.api.user.form.PageQuery;
import com.xuebusi.toutiao.admin.api.user.form.RegisterForm;
import com.xuebusi.toutiao.admin.api.user.form.UserForm;
import com.xuebusi.toutiao.admin.api.user.service.FollowerService;
import com.xuebusi.toutiao.admin.api.user.service.SourceService;
import com.xuebusi.toutiao.admin.api.user.service.UserService;
import com.xuebusi.toutiao.admin.api.user.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 用户服务控制器
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/mp/v1_0")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private FollowerService followerService;

    @Resource
    private SourceService sourceService;

    /**
     * 登录认证
     *
     * @param loginForm
     * @return
     */
    @PostMapping("/authorizations")
    public ResponseEntity<ApiResult<UserTokenVO>> authorizations(@RequestBody @Valid LoginForm loginForm) {
        return userService.authorizations(loginForm);
    }
    @GetMapping("/authorizations")
    public String authorizationsTest() {
        System.out.println("2222222");
        return "牛逼";
    }


    /**
     * 刷新token
     *
     * @param auth
     * @return
     */
    @PutMapping("/authorizations")
    public ResponseEntity<ApiResult<RefreshTokenVO>> refreshToken(@RequestHeader("Authorization") String auth) {
        return userService.refreshToken(auth);
    }

    /**
     * 用户注册
     *
     * @param registerForm
     * @return
     */
    @PostMapping("/register")
    public ResponseEntity<ApiResult<UserEntity>> register(@RequestBody @Valid RegisterForm registerForm) {
        return userService.register(registerForm);
    }

    /**
     * 上传用户头像
     *
     * @param file
     * @param request
     * @return
     */
    @PatchMapping("/user/photo")
    public ResponseEntity<ApiResult<UserPhotoVO>> uploadUserPhoto(
            @LoginUser UserEntity loginUser,
            @RequestParam("photo") MultipartFile file,
            HttpServletRequest request) {
        return userService.updatePhoto(file, loginUser.getId(), request);
    }

    /**
     * 修改用户资料
     *
     * @param loginUser
     * @param userForm
     * @return
     */
    @PatchMapping("/user/profile")
    public ResponseEntity<ApiResult<UserProfileVO>> profile(
            @LoginUser UserEntity loginUser, @RequestBody UserForm userForm) {
        return userService.updateUserProfile(loginUser.getId(), userForm);
    }

    /**
     * 获取用户资料
     *
     * @param loginUser
     * @return
     */
    @GetMapping("/user/profile")
    public ResponseEntity<ApiResult<UserDetailVO>> profile(@LoginUser UserEntity loginUser) {
        return userService.getUserProfile(loginUser.getId());
    }

    /**
     * 获取粉丝列表
     *
     * @param pageQuery 分页参数
     * @return
     */
    @GetMapping("/followers")
    public ResponseEntity<ApiResult<PageInfo<FollowerVO>>> followers(
            @LoginUser UserEntity loginUser, PageQuery pageQuery) {
        return followerService.followers(loginUser.getId(), pageQuery.getPage(), pageQuery.getPer_page());
    }

    /**
     * 收藏或取消收藏图片素材
     *
     * @param loginUser  登录用户
     * @param sourceId   资源id
     * @param collectPVO 是否收藏
     * @return
     */
    @PutMapping("/user/images/{target}")
    public ResponseEntity<ApiResult<CollectRVO>> collect(
            @LoginUser UserEntity loginUser,
            @PathVariable("target") Integer sourceId,
            @RequestBody @Valid CollectPVO collectPVO) {
        return userService.collect(loginUser.getId(), sourceId, collectPVO.getCollect());
    }

    /**
     * 上传图片素材
     *
     * @param loginUser 登录用户
     * @param file      图片文件
     * @param request   request
     * @return
     */
    @PostMapping("/user/images")
    public ResponseEntity<ApiResult<SourceEntity>> uploadImage(
            @LoginUser UserEntity loginUser,
            @RequestParam("image") MultipartFile file,
            HttpServletRequest request) {
        return userService.uploadImage(loginUser.getId(), file, request);
    }

    /**
     * 删除图片素材
     *
     * @param loginUser 登录用户
     * @param sourceId  资源id
     * @return
     */
    @DeleteMapping("/user/images/{target}")
    public ResponseEntity deleteImage(@LoginUser UserEntity loginUser, @PathVariable("target") Integer sourceId) {
        return userService.deleteImage(loginUser.getId(), sourceId);
    }

    /**
     * 分页查询用户图片素材
     *
     * @param loginUser      登录用户
     * @param sourceQueryPVO 查询条件
     * @return
     */
    @GetMapping("/user/images")
    public ResponseEntity<PageInfo<SourceRVO>> getImages(@LoginUser UserEntity loginUser, SourceQueryPVO sourceQueryPVO) {
        return sourceService.getImages(loginUser.getId(), sourceQueryPVO);
    }
}
